<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
    defined ('IN_AGORA') or die;

	class TaskController extends AgoraController {
		
		function __construct() {
			parent::__construct();
			$this->loadDefaultView();
		}

		function gmap() {
			$this->view->template = 'misc/gmap';
		}

		function rules() {
		    $this->view->assign('some_tabs_off',true);
			$this->view->template = 'misc/rules';
		}
		
		function checkEmail() {
			$email = Agora::getVar('email');
			$isValid = true;
			$atIndex = strrpos($email, "@");
			if (is_bool($atIndex) && !$atIndex) {
				$isValid = false;
			}
			else {
				$domain = substr($email, $atIndex+1);
				$local = substr($email, 0, $atIndex);
				$localLen = strlen($local);
				$domainLen = strlen($domain);
				if (1 > $localLen || 64 < $localLen) {
					// local part length exceeded
					$isValid = false;
				}
				else if (1 > $domainLen || 255 < $domainLen) {
					// domain part length exceeded
					$isValid = false;
				}
				else if ('.' == $local[0] || '.' == $local[$localLen-1]) {
					// local part starts or ends with '.'
					$isValid = false;
				}
				else if (preg_match('/\.{2}/', $local)) {
					// local part has two consecutive dots
					$isValid = false;
				}
				else if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain)) {
					// character not valid in domain part
					$isValid = false;
				}
				else if (preg_match('/\.{2}/', $domain)) {
					// domain part has two consecutive dots
					$isValid = false;
				}
				else if (!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\","",$local))) {
					// character not valid in local part unless
					// local part is quoted
					if (!preg_match('/^"(\\"|[^"])+"$/', str_replace("\\","",$local))) {
						$isValid = false;
					}
				}
				if (function_exists('checkdnsrr') && is_callable('checkdnsrr')) {
					if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
						// domain not found in DNS
						$isValid = false;
					}
				}
				if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
					$isValid = false;
				}
			}
			// output JSON data
			$msg = $isValid ? '' : JText::_('COM_AGORA_EMAIL_NOT_VALID');
			header('Content-type: application/json');
			echo json_encode(array('valid' => $isValid, 'msg' => $msg));
			jexit();
		}

		function captcha() {
			ainclude('include|kcaptcha|kcaptcha.php');
			$captcha = new KCAPTCHA();
			Agora::setSessionVar('captcha',$captcha->getKeyString());
			JRequest::setVar('type','raw');
			JRequest::setVar('no_html','1');
			jexit();
		}

		function map() {

			$board_model = & Model::getInstance('BoardModel');
			$forums = $board_model->loadForumList();
			$forum_model = & Model::getInstance('ForumModel');
			
            foreach($forums as $cat => $cat_forums) {
            	foreach ($cat_forums['forums'] as $key=>$forum) {
	            	$forums[$cat][$key]['topics'] = $forum_model->loadTopics($forum['id']);
	            }
            }
			
			$this->view->smarty->assign('categories',$forums);
			$this->display("misc/map");
		}

		function markRead() {
			$topic_model = & Model::getInstance('TopicModel');
			$topic_model->markAllRead($this->agora_user['id'], $this->agora_config['o_timeout_visit'] * 86400);
	
	        if (isset($_SERVER['HTTP_REFERER'])) {
				Agora::redirect($_SERVER['HTTP_REFERER']);
	        } else {
	        	Agora::redirect(Agora::makeURL());
		    }
		}
	}
